import { _decorator, Component, Input, input, tween, v3, } from 'cc';
import { bridegsRootController } from './bridegsRootController';
import { playerController } from './playerController';
import { piersRootController } from './piersRootController';
import { bridgeController } from './bridgeController';
const { ccclass, property } = _decorator;

@ccclass('RootController')
export class RootController extends Component {
    isMove:boolean = true;
    isEnd:boolean = false;
    start() 
    {
        this.Touch();
        this.playerDie();
        this.playerSurvival();
    }
    init()
    {
    }
    Touch()
    {
        this.node.parent.on(Input.EventType.TOUCH_START,()=>
        {
            if(this.isMove)
            {
                this.isEnd = true;
                this.isMove = false;
                console.log("点击")
                this.node.getChildByName("bridgesRoot").getComponent(bridegsRootController).createBridge();
            }
            
        },this)

        this.node.parent.on(Input.EventType.TOUCH_END,()=>
        {
            if(this.isEnd)
            {
                this.isEnd = false;
                this.node.getChildByName("bridgesRoot").getComponent(bridegsRootController).UpOk();
                //移动玩家
                setTimeout(()=>
                {
                    this.node.getChildByName("player").getComponent(playerController).IsMove = true;
                },700)
            }
            
        },this)
    }
    //监听玩家是否死亡
    playerDie()
    {
        this.node.getChildByName("player").on('die',()=>
        {
            console.log('死亡')
            const player = this.node.getChildByName('player')
            const bridges = this.node.getChildByName('bridgesRoot').children
            const lastbridge = bridges[bridges.length - 1]
            const playerX = player.getPosition().x
            const bridgeX = lastbridge.getPosition().x
            tween(player)
                .to(0.5,{position:v3(playerX,-360,0)},{easing:'quadIn'})
                .start()
            tween(lastbridge)
                .to(0.5,{position:v3(bridgeX,-360,0)},{easing:'quadIn'})
                .start();
            setTimeout(()=> 
            {
                player.getComponent(playerController).die();
            }, 500);
            setTimeout(()=>
            {
                lastbridge.getComponent(bridgeController).die();
            },600)
        },this)
    }
    //监听本次玩家是否存活
    playerSurvival()
    {   
        this.node.getChildByName('player').on('survival',()=>
        {
            console.log('本次执行存活')
            this.node.getChildByName('piersRoot').getComponent(piersRootController).create();
            this.backMove();
        },this)
    }
    //场景后移
    backMove()
    {
        const piers = this.node.getChildByName("piersRoot").children
        const bridges = this.node.getChildByName("bridgesRoot").children
        const player = this.node.getChildByName('player')
        const dx = piers[1].getPosition().x - piers[0].getPosition().x;
        for(let i = 0;i < piers.length;i++)
        {
            tween(piers[i])
                .to(0.5,{position:v3(piers[i].getPosition().x - dx,piers[i].getPosition().y,0)})
                .start();
            console.log('aaa')
        }
        for(let i = 0;i < bridges.length;i++)
        {
            tween(bridges[i])
                .to(0.5,{position:v3(bridges[i].getPosition().x - dx,bridges[i].getPosition().y,0)})
                .start()
                console.log('bbb')
        }
        tween(player)
            .to(0.5,{position:v3(player.getPosition().x - dx,player.getPosition().y,0)})
            .start();
        setTimeout(()=>
        {
            if(this.node.getChildByName('player').getComponent(playerController).score !== 5)
            {
                this.isMove = true
            }
            this.node.emit('moveOk')
        },600)
    }

    update(deltaTime: number) {
        
    }
}


